/**
 * 
 */
package com.geezdata.cps.ttk.query.query.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.ttk.common.request.QueryJobListRequest;
import com.geezdata.cps.ttk.common.response.QueryJobListResult;
import com.geezdata.cps.ttk.common.vo.JobResVO;
import com.geezdata.cps.ttk.domain.domain.Job;
import com.geezdata.cps.ttk.mapper.mapper.JobMapper;
import com.geezdata.cps.ttk.query.query.JobService;
import com.google.common.collect.Maps;

/**
 * @author sszheng
 * @date 创建时间：2017年10月19日 下午4:26:33
 *
 */
@Service
public class JobServiceImpl implements JobService {
	private static final Logger logger = LoggerFactory.getLogger(JobServiceImpl.class);

	@Autowired
	private JobMapper jobMapper;
	@Override
	public Job getJobByTriggerName(String triggerName) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("triggerName", triggerName);
		List<Job> jobList = jobMapper.selectBatch(map);
		if(CollectionUtils.isEmpty(jobList)) {
			logger.error("无此job, triggerName=" + triggerName);
			return null;
		} else if(jobList.size() >= 2) {
			logger.error("重复的job, triggerName=" + triggerName);
			throw new MyException(ErrorCode.INTERNAL_SERVER_ERROR);
		}
		return jobList.get(0);
	}
	
	@Override
	public QueryJobListResult getJobList(QueryJobListRequest request) {
		QueryJobListResult result = new QueryJobListResult();
		
		Map<String, Object> params = Maps.newHashMap();
		params.put("triggerGroup", request.getTriggerGroup());
		params.put("status", request.getStatus());
		params.put("sendType", request.getSendType());
		
		List<Job> jobList = jobMapper.selectBatch(params);
		if(CollectionUtils.isEmpty(jobList)) {
			return result;
		}
		
		List<JobResVO> jobVOList = BeanUtil.convertList(jobList, JobResVO.class);
		result.setJobList(jobVOList);
		return result;
	}
}
